//
//  MLElementButtonView.m
//  CustomerPo
//
//  Created by molon on 8/12/14.
//  Copyright (c) 2014 molon. All rights reserved.
//

#import "MLElementButtonView.h"
#import "MLElementButtonItem.h"
#import "UIView+MLKit.h"

@interface MLElementButtonView()

@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation MLElementButtonView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.exclusiveTouch = YES;
    }
    return self;
}

#pragma mark - event
- (void)buttonEvent:(id)sender
{
    if (self.elementItem.customClickBlock) {
        self.elementItem.customClickBlock(self,self.elementItem);
    }else if (self.elementItem.combination.clickBlock){
        self.elementItem.combination.clickBlock(self,self.elementItem);
    }
}

#pragma mark - setter and getter
- (UIButton*)button
{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.contentMode = UIViewContentModeScaleAspectFit;
        _button.backgroundColor = [UIColor clearColor];
        _button.clipsToBounds = YES;
        _button.exclusiveTouch = YES;
        
        [_button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_button];
    }
    return _button;
}

- (UILabel*)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.clipsToBounds = YES;
        _titleLabel.numberOfLines = 1;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
        
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (void)setElementItem:(MLElementButtonItem *)elementItem
{
    _elementItem = elementItem;
    
    if (elementItem.combination.isUseNameAsImage) {
        [self.button setTitle:elementItem.name forState:UIControlStateNormal];
        [self.button setImage:nil forState:UIControlStateNormal];
    }else{
        [self.button setImage:[UIImage imageNamed:elementItem.imageName] forState:UIControlStateNormal];
        [self.button setTitle:nil forState:UIControlStateNormal];
    }
    if (!elementItem.combination.isNotNeedDisplayTitle) {
        self.titleLabel.text = elementItem.name;
    }
    if (elementItem.combination.buttonContentMode!=self.button.contentMode) {
        self.button.contentMode = elementItem.combination.buttonContentMode;
        if (self.button.contentMode==UIViewContentModeScaleAspectFill) {
            self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
            self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        }else{
            self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        }
    }
    if (elementItem.combination.customTitleColor) {
        self.titleLabel.textColor = elementItem.combination.customTitleColor;
    }
    if (elementItem.combination.bkgImageName.length>0) {
        [self.button setBackgroundImage:[UIImage imageNamed:elementItem.combination.bkgImageName] forState:UIControlStateNormal];
    }
    [self setNeedsLayout];
}

#pragma mark - layout
- (void)layoutSubviews
{
    [super layoutSubviews];
    
#define kTitleLabelHeight 15.0f
#define kTitleLabelTopMargin 5.0f
    
    CGFloat buttonHeight = self.frameHeight;
    if (!self.elementItem.combination.isNotNeedDisplayTitle) {
        buttonHeight-=(kTitleLabelHeight+kTitleLabelTopMargin);
    }
    
    self.button.frame = CGRectMake(0, 0, self.frameWidth, buttonHeight);
    
    if (self.elementItem.combination.isNotNeedDisplayTitle) {
        self.titleLabel.frame = CGRectZero;
    }else{
        self.titleLabel.frame = CGRectMake(0, self.button.frameBottom+kTitleLabelTopMargin, self.button.frameWidth, kTitleLabelHeight);
    }
}


@end
